package de.tc.stammdatenverwaltung.mailing;

import de.tc.stammdatenverwaltung.functions.PropertiesHandler;
import de.tc.stammdatenverwaltung.functions.commonFunctions;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.util.Iterator;
import java.util.Vector;
import javax.mail.MessagingException;
import javax.swing.JOptionPane;

public class mailEditor extends javax.swing.JDialog {

  PropertiesHandler ph = new PropertiesHandler();
  Vector <String> recievers = new Vector<String>();
  String plainContentBackup = "";
  String htmlContentBackup = "";
  EkitWrapper writer = null;

  /** Creates new form mailEditor */
  public mailEditor(java.awt.Frame parent, boolean modal, Vector<String> rec) {
      super(parent, modal);
      setLocation(getXPos(), 0);
      recievers = rec;
      initComponents();
      reset();
      setTitle("Mail verfassen ...");
  }

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    buttonGroup1 = new javax.swing.ButtonGroup();
    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jTextField1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jTextField2 = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jPanel2 = new javax.swing.JPanel();
    jRadioButton1 = new javax.swing.JRadioButton();
    jRadioButton2 = new javax.swing.JRadioButton();
    jLabel4 = new javax.swing.JLabel();
    jButton5 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

    jButton1.setText("Senden");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    jButton2.setText("Zurücksetzen");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton2ActionPerformed(evt);
      }
    });

    jButton3.setText("Fußzeile einfügen");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton3ActionPerformed(evt);
      }
    });

    jButton4.setText("als Fußzeile speichern");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton4ActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(18, 18, 18)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
          .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE))
        .addGap(18, 18, 18)
        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
          .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
          .addGroup(jPanel1Layout.createSequentialGroup()
            .addComponent(jButton4)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
            .addComponent(jButton3)))
        .addContainerGap())
    );

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jTextArea1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    jScrollPane1.setViewportView(jTextArea1);

    jLabel1.setText("Betreff");

    jLabel2.setText("Inhalt");

    jLabel3.setText("Empfänger");

    jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

    buttonGroup1.add(jRadioButton1);
    jRadioButton1.setSelected(true);
    jRadioButton1.setText("Normaler Text");
    jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButton1ActionPerformed(evt);
      }
    });

    buttonGroup1.add(jRadioButton2);
    jRadioButton2.setText("HTML - Mail");
    jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButton2ActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addComponent(jRadioButton1)
        .addGap(18, 18, 18)
        .addComponent(jRadioButton2)
        .addContainerGap(345, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
        .addComponent(jRadioButton1)
        .addComponent(jRadioButton2))
    );

    jLabel4.setText("Mail-Typ");

    jButton5.setText("Text vom Editor Übernehmen");
    jButton5.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton5ActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addGroup(layout.createSequentialGroup()
        .addComponent(jLabel4)
        .addContainerGap())
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 596, Short.MAX_VALUE)
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 547, Short.MAX_VALUE))
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
            .addComponent(jLabel2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 555, Short.MAX_VALUE))
          .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 596, Short.MAX_VALUE)
          .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 596, Short.MAX_VALUE)
          .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(jButton5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 596, Short.MAX_VALUE))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(4, 4, 4)
        .addComponent(jLabel4)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jLabel3)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jLabel1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jLabel2)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jButton5))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton3ActionPerformed
    {//GEN-HEADEREND:event_jButton3ActionPerformed
      jTextArea1.setText(jTextArea1.getText() + "\r\n" + ph.getProperty("mail-footer") );
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton4ActionPerformed
    {//GEN-HEADEREND:event_jButton4ActionPerformed
      ph.setProperty( "mail-footer", jTextArea1.getText() );
      ph.setProperties();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
      Vector <String> edRecievers = jTextEditTostrVector();
      
      try
      {
        boolean auth = ph.getProperty("mail-s-auth").equals("1");
        int  authart = Integer.parseInt( ph.getProperty("mail-s-authart") );
        mailSender.postMail(edRecievers, jTextField1.getText(), jTextArea1.getText(),
          ph.getProperty("mail-s-abs"), ph.getProperty("mail-s-server"), ph.getProperty("mail-s-port"),
          ph.getProperty("mail-s-user"), ph.getProperty("mail-s-pass"), auth, authart, jRadioButton2.isSelected() );

        commonFunctions.loggeMails( edRecievers, "Betreff:" + jTextField1.getText() + "\r\nInhalt:\r\n" + jTextArea1.getText() );
        JOptionPane.showMessageDialog(null, "Mail erfolgreich versandt.", "Erfolg!", JOptionPane.PLAIN_MESSAGE);
      }
      catch (MessagingException ex)
      {
        JOptionPane.showMessageDialog(null, "Das Versenden der Mail ist fehlgeschlagen\r\n" +
          ex.getLocalizedMessage(), "Fehler!", 1);
        ex.printStackTrace();
      }
      catch (NumberFormatException nfe)
      {
        JOptionPane.showMessageDialog(null, "Fehler in den Einstellungen!\r\n" +
          nfe.getLocalizedMessage(), "Fehler!", 1);
        nfe.printStackTrace();
      }
      catch (Exception e)
      {
        JOptionPane.showMessageDialog(null, "Sonstiger Fehler:\r\n" +
          e.getLocalizedMessage(), "Fehler!", 1);
        e.printStackTrace();
      }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
    {//GEN-HEADEREND:event_jButton2ActionPerformed
      reset();
      
      if ( writer != null)
      {
        writer.dispose();
        writer = null;
      }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jRadioButton2ActionPerformed
    {//GEN-HEADEREND:event_jRadioButton2ActionPerformed
      plainContentBackup = jTextArea1.getText();
      jTextArea1.setText("Wenn die Bearbeitung des Texts im neu ge\u00F6ffneten Editor fertig ist, \r\n" +
              "bitte auf \"Text vom Editor \u00DCbernehmen\" klicken.");
      jButton5.setVisible(true);

      writer = new EkitWrapper(htmlContentBackup);
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jRadioButton1ActionPerformed
    {//GEN-HEADEREND:event_jRadioButton1ActionPerformed
      if ( !jTextArea1.getText().equals("Wenn die Bearbeitung des Texts im neu ge\u00F6ffneten Editor fertig ist, \r\n" +
              "bitte auf \"Text vom Editor \u00DCbernehmen\" klicken.") )
        htmlContentBackup = jTextArea1.getText();

      jTextArea1.setText(plainContentBackup);
      jButton5.setVisible(false);
      if ( writer != null)
      {
        writer.dispose();
        writer = null;
      }
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton5ActionPerformed
    {//GEN-HEADEREND:event_jButton5ActionPerformed
      jTextArea1.setText( writer.getDocument() );
    }//GEN-LAST:event_jButton5ActionPerformed

    private void reset()
    {
      jRadioButton1.doClick();

      jTextArea1.setText( ph.getProperty("mail-footer") );
      jTextArea1.requestFocus();
      strVectorTojTextEdit(recievers);
      jTextField1.setText("");

      jButton5.setVisible(false);

      plainContentBackup = jTextArea1.getText();
      htmlContentBackup = jTextArea1.getText();
    }

    private int getXPos()
    {
      Dimension foo = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getSize();
      int x = foo.width - this.getWidth();
      
      if (x > 750)
        return 750;

      return x;
    }

    private void strVectorTojTextEdit(Vector<String> v)
    {
      String recString = "";
      for (Iterator<String> it = recievers.iterator(); it.hasNext();)
      {
        String string = it.next();

        recString = recString.concat(string);

        if ( it.hasNext() )
          recString = recString.concat("; ");
      }

      jTextField2.setText(recString);
    }

    private Vector<String> jTextEditTostrVector()
    {
      Vector<String> s =  new Vector<String>();

      String[] ar = jTextField2.getText().split("; ");
      //Groesse schon vorher setzen, spart rechenzeit
      s.setSize(ar.length);

      for (int i = 0; i < ar.length; i ++)
      {
        if (ar[i].trim().length() > 0)
          s.add(ar[i]);
      }

      return s;
    }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.ButtonGroup buttonGroup1;
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JButton jButton3;
  private javax.swing.JButton jButton4;
  private javax.swing.JButton jButton5;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JRadioButton jRadioButton1;
  private javax.swing.JRadioButton jRadioButton2;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextArea jTextArea1;
  private javax.swing.JTextField jTextField1;
  private javax.swing.JTextField jTextField2;
  // End of variables declaration//GEN-END:variables

}
